
package it.allerj.analysis;

import blobDetection.BlobDetection;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

abstract class Detector {
    
    protected int image_w;
    protected int image_h;
    protected BlobFilter blobFilter;
    protected ArrayList<DotMark> dotMarksFound;
    
    private int[] rgbs;
    
    public Detector(BufferedImage img, BlobFilter blobFilter) {
        
        // store BufferedImage dimensions
        image_w = img.getWidth();
        image_h = img.getHeight();

        // convert BufferedImage to pixels array
        this.rgbs = new int[image_w*image_h];
        img.getRGB(0, 0, image_w, image_h, rgbs, 0, image_w);
        
        // store blob filter
        this.blobFilter = blobFilter;

        dotMarksFound = new ArrayList<DotMark>();
    }
    
    public boolean process(float threshold) {
        
        // empty DotMarks container
        dotMarksFound.clear();
        
        // instance a blob detection object
        BlobDetection bd = new BlobDetection(image_w, image_h);
        
        // setup blob detection system
        
        // search for DARK areas
        bd.setPosDiscrimination(false);
        bd.activeCustomFilter(blobFilter);
        bd.setThreshold(threshold);

        // actually search for blobs
        bd.computeBlobs(rgbs);
        
        // polymorphic hook to check data consistancy 
        return checkBlobDetection(bd);
    }
    
    public ArrayList<DotMark> getDotMarksFound() {
        return dotMarksFound;
    }

    protected abstract boolean checkBlobDetection(BlobDetection bd);

}